﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Doan.Website.Models;
using Ext.Net;
using Ext.Net.MVC;
using System.Linq.Dynamic;

namespace Doan.Website.Controllers
{
    [DirectController]
    public class HospitalController : Controller
    {
        //Hospital
        private static DoAnWebsiteEntities db = new DoAnWebsiteEntities();

        [DirectMethod]
        public ActionResult DeleteHospital(int id)
        {
            var deleteItem = db.Hospitals.SingleOrDefault(x => x.HospitalId == id);
            db.Hospitals.DeleteObject(deleteItem);
            db.SaveChanges();
            var store = X.GetCmp<Store>("HospitalStore");
            store.Reload();
            return this.FormPanel();
        }


        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult ListHospital()
        {
            Session["includeInActiveHospital"] = false;
            Session["CharFilterHospital"] = "";
            return View();
        }

        public ActionResult IncludeInActive()
        {
            var store = X.GetCmp<Store>("HospitalStore");

            Session["includeInActiveHospital"] = !(bool)Session["includeInActiveHospital"];

            store.Reload();
            return this.Direct();
        }

        [DirectMethod]
        public ActionResult CharFilterHospital(string filterstring)
        {
            var store = X.GetCmp<Store>("HospitalStore");
            Session["CharFilterHospital"] = filterstring;
            store.Reload();
            return this.Direct();
        }

        public ActionResult BindingHospitalStore(StoreRequestParameters parameters)
        {
            var sampleData = db.Hospitals.Select(m=> new
                                                         {
                                                             m.HospitalId,
                                                             m.Name,
                                                             m.Phone,
                                                             m.Description,
                                                             m.Address,
                                                             m.IsActive
                                                         });


            bool includeInActive = (bool)Session["includeInActiveHospital"];
            string charFilter = (string)Session["CharFilterHospital"];

            var result = sampleData.Where(ent => ((includeInActive || (bool)ent.IsActive)));
            if ("0".Equals(charFilter))
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.IsActive) &&
                    (ent.Name.StartsWith("0") || ent.Name.StartsWith("1") ||
                    ent.Name.StartsWith("2") || ent.Name.StartsWith("3") ||
                    ent.Name.StartsWith("4") || ent.Name.StartsWith("5") ||
                    ent.Name.StartsWith("6") || ent.Name.StartsWith("7") ||
                    ent.Name.StartsWith("8") || ent.Name.StartsWith("9")
                    )));
            }

            else
            {
                result = sampleData.Where(ent => ((includeInActive || (bool)ent.IsActive) && ent.Name.StartsWith(charFilter)));

            }

            // Sorting
            if (!string.IsNullOrEmpty(parameters.SimpleSort))
            {
                result = result.OrderBy(parameters.SimpleSort + (parameters.SimpleSortDirection == SortDirection.DESC ? " desc" : " asc"));
            }
            else
            {
                result = result.OrderBy(ent => ent.HospitalId);
            }

            // Paging
            int limit = parameters.Limit;
            if ((parameters.Start + parameters.Limit) > result.Count())
            {
                limit = result.Count() - parameters.Start;
            }

            var rangeData = (parameters.Start < 0 || limit < 0) ? result : result.Skip(parameters.Start).Take(limit);
            return this.Store(rangeData.Select(ent => new { ent.HospitalId, ent.Name, ent.Phone, ent.Description, ent.Address, ent.IsActive }), result.Count());
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult CreateHospital()
        {
            ViewBag.HideTextBox = false;
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult CreateHospital(Hospital model)
        {
            if (ModelState.IsValid)
            {
                model.IsActive = true;
                db.AddToHospitals(model);
                db.SaveChanges();
                X.Msg.Notify("Thành công", "Tạo mới thành công!").Show();
                return this.FormPanel();
            }

            X.Msg.Notify("Lỗi", "Tạo mới không thành công!").Show();

            return this.FormPanel();
        }

        [HttpGet]
        [AccessDeniedAuthorize(Roles = "administrators, users")]
        public ActionResult EditHospital(int id)
        {
            ViewBag.HideTextBox = false;
            Hospital hospital = db.Hospitals.Single(m => m.HospitalId == id);

            return View(hospital);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult EditHospital(Hospital model)
        {
            Hospital hospital1 = db.Hospitals.Single(c => c.HospitalId == model.HospitalId);
            if (ModelState.IsValid)
            {
                db.Hospitals.Detach(hospital1);
                db.Hospitals.Attach(model);
                db.ObjectStateManager.ChangeObjectState(model, EntityState.Modified);
                db.SaveChanges();
                X.Msg.Notify("Thành công", "Sửa thành công!").Show();
                return this.FormPanel();
            }
            X.Msg.Notify("Lỗi", "Sửa không thành công!").Show();
            return this.FormPanel();
        }
    }
}
